package com.faxsun.core.catalog.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.broadleafcommerce.common.BroadleafEnumerationType;
import org.broadleafcommerce.profile.core.dao.CountryDao;
import org.broadleafcommerce.profile.core.domain.Country;
import org.springframework.stereotype.Component;

/**
 * 国家枚举类型
 * 
 * @author zhaoteng.song@faxsun.com
 *
 */
@Component
public class CountryEnum implements BroadleafEnumerationType, Serializable,
		Comparable<CountryEnum> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1543030544363956726L;

	private static final String[] frequence = { "CN", "GB", "US", "NZ", "AU",
			"CA", "DE", "FR" };
	private static List<String> frequentlyList = null;

	protected Country country;

	@Resource(name = "blCountryDao")
	protected CountryDao countryDao;

	private static final LinkedHashMap<String, CountryEnum> TYPES = new LinkedHashMap<String, CountryEnum>();

	public static CountryEnum getInstance(final String type) {
		return TYPES.get(type);
	}

	@Override
	public String getType() {
		return country.getAbbreviation();
	}

	@Override
	public String getFriendlyType() {
		return country.getName();
	}

	/**
	 * 从数据库中加载所有国家 2015-03-16 将常用到的国家放在前面：中国、英国、美国、新西兰、澳大利亚、加拿大、德国、法国
	 */
	public void init() {
		List<Country> countries = countryDao.findCountries();
		frequentlyList = new ArrayList<String>(Arrays.asList(frequence));
		Collections.sort(countries, new CountryComparator());
		for (Country c : countries) {
			CountryEnum enumVar = new CountryEnum();
			enumVar.country = c;
			if(c.getAbbreviation().equals("")){
				continue;
			}
			TYPES.put(c.getAbbreviation(), enumVar);
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((country == null) ? 0 : country.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CountryEnum other = (CountryEnum) obj;
		if (country == null) {
			if (other.country != null)
				return false;
		} else if (!country.equals(other.country))
			return false;
		return true;
	}

	public CountryDao getCountryDao() {
		return countryDao;
	}

	public void setCountryDao(CountryDao countryDao) {
		this.countryDao = countryDao;
	}

	/**
	 * 国家实体比较器，用于将常用国家放在前面，其他国家按汉字顺序排列
	 * 
	 * @author zhaoteng.song@faxsun.com
	 *
	 */
	class CountryComparator implements Comparator<Country> {

		// 期望排在前面的国家代码

		@Override
		public int compare(Country o1, Country o2) {

			return compareCountry(o1, o2);
		}
	}

	@Override
	public int compareTo(CountryEnum o) {

		return compareCountry(this.country, o.country);

	}

	private int compareCountry(Country o1, Country o2) {
		String abbr1 = o1.getAbbreviation();
		String abbr2 = o2.getAbbreviation();
		
		boolean isO1Target = frequentlyList.contains(abbr1);
		boolean isO2Target = frequentlyList.contains(abbr2);

		if (isO1Target && isO2Target) {
			int i1 = frequentlyList.indexOf(abbr1);
			int i2 = frequentlyList.indexOf(abbr2);
			return i1 - i2;
		} else if (isO1Target && !isO2Target) {
			return -1;
		} else if (!isO1Target && isO2Target) {
			return 1;
		}

		return o1.getName().compareTo(o2.getName());
	}
}
